<template>
	<view class="methodpay">
		<view class="macon bgbottom">
			<view @click="changeflag(2)">
				<view>
					<image src="../../static/images/zfb1.png"></image>
					<text>支付宝支付</text>
				</view>
				<image src="/static/images/z44.png" v-if="choosenum==2 || choosenum==6"></image>
				<image src="/static/images/z43.png" v-else></image>
			</view>
			<view @click="changeflag(1)">
				<view>
					<image src="../../static/images/weixin.png"></image>
					<text>微信支付</text>
				</view>
				<image src="/static/images/z44.png" v-if="choosenum==1 || choosenum==4"></image>
				<image src="/static/images/z43.png" v-else></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data(){
			return{
				 choosenum:2, // 1微信公众号 3微信支付宝  4微信App  6支付宝App
			}
		},
		props:['account','type','recharge_id'],
		created() {
			// #ifdef H5
			  if(window.location.href.indexOf('code')!=-1){
				  var code = window.location.href.split('?')[1].split('&state')[0].split('code=')[1];
				  this.request('/wx/getCodeToken',{code:code}).then(res => {
					if(res.data.code==1){
						this.request('/member/bindOpenId',{open_id:res.data.data}).then(res => {
							if(res.data.code==1){
								this.choosenum = 1
								this.payment()
							}
						})	
					}
				  })
			}
			 // #endif
		},
		methods:{
			changeflag(n){
				this.choosenum=n
			},
			payment(){
				var that = this
				// #ifdef APP-PLUS
					if(this.choosenum==1){
						this.choosenum=4
					}
					if(this.choosenum==2){
						this.choosenum=6
					}
				// #endif
				// #ifdef H5
				if(this.choosenum==1){
					this.choosenum = 11
					// this.request('/member/getIsOpenId').then(res => {
					// 	if(res.data.code==0){
					// 		this.request('/wx/getPayWxLogin',{current_url:window.location.href.split('#')[1]}).then(res => {
					// 			if(res.data.code==1){
					// 				window.location.href=res.data.data.replace(/\amp%3B/g, '')	
					// 			}
					// 		})		
					// 	}
					// })		 
				}
				// #endif
				this.request('/order/toRechargePay',{
					pay_type:this.choosenum,
					amount:this.account,
					type:this.type,
					recharge_id:this.recharge_id
				}).then((res) => {
					  if(res.data.code==1){
						this.$tip(res.data.msg)
						if(this.choosenum==1){
							window.WeixinJSBridge.invoke(
							'getBrandWCPayRequest', {
								"debug":true,
								"appId": res.data.data.payment.app_id, //公众号名称，由商户传入
								"timeStamp": res.data.data.payment.timeStamp, //时间戳，自1970年以来的秒数
								"nonceStr": res.data.data.payment.nonceStr, //随机串
								"package": 'prepay_id='+res.data.data.payment.prepay_id,
								"signType": 'MD5', //微信签名方式：
								"paySign": res.data.data.payment.paySign //微信签名
							},
							function (res) {
								if (res.err_msg == "get_brand_wcpay_request:ok") {
									setTimeout(()=>{
										if(that.type==1){
											uni.redirectTo({
												url:'/pages/personal/invest'
											})
										}else if(that.type==2){
											uni.redirectTo({
												url:'/pages/personal/lingpai'
											})
										}
									},500)
								}
								if (res.err_msg == "get_brand_wcpay_request:fail") {
									setTimeout(()=>{
										if(that.type==1){
											uni.redirectTo({
												url:'/pages/personal/invest'
											})
										}else if(that.type==2){
											uni.redirectTo({
												url:'/pages/personal/lingpai'
											})
										}
									},500)
								}
								if (res.err_msg == "get_brand_wcpay_request:cancel") {
									setTimeout(()=>{
										if(that.type==1){
											uni.redirectTo({
												url:'/pages/personal/invest'
											})
										}else if(that.type==2){
											uni.redirectTo({
												url:'/pages/personal/lingpai'
											})
										}
									},500)
								}
								if (res.err_msg == "total_fee") {
									setTimeout(()=>{
										if(that.type==1){
											uni.redirectTo({
												url:'/pages/personal/invest'
											})
										}else if(that.type==2){
											uni.redirectTo({
												url:'/pages/personal/lingpai'
											})
										}
									},500)
									// layer.msg("1、请检查预支付会话标识prepay_id是否已失效,2、请求的appid与下单接口的appid是否一致");
								}
							});
						}else if(this.choosenum==2 || this.choosenum==11){
							window.location.href=res.data.data.payment.jump_url
						}else if(this.choosenum==4){
							let obj = {
								 appid: res.data.data.payment.appid,
								 noncestr: res.data.data.payment.noncestr,
								 package: 'Sign=WXPay', // 固定值，以微信支付文档为主
								 partnerid: res.data.data.payment.partnerid,
								 prepayid: res.data.data.payment.prepayid,
								 timestamp: res.data.data.payment.timestamp,
								 sign: res.data.data.payment.sign // 根据签名算法生成签名
							}
							uni.requestPayment({
								provider: 'wxpay',
								orderInfo: obj, //微信、支付宝订单数据
								success: function (res) {
									setTimeout(()=>{
										if(that.type==1){
											uni.redirectTo({
												url:'/pages/personal/invest'
											})
										}else if(that.type==2){
											uni.redirectTo({
												url:'/pages/personal/lingpai'
											})
										}
									},500)
								},
								fail: function (err) {
									
								}
							});
						}else if(this.choosenum==6){
							uni.requestPayment({
								provider: 'alipay',
								orderInfo:res.data.data.payment,
								success: function (res) {
									setTimeout(()=>{
										if(that.type==1){
											uni.redirectTo({
												url:'/pages/personal/invest'
											})
										}else if(that.type==2){
											uni.redirectTo({
												url:'/pages/personal/lingpai'
											})
										}
									},500)
								},
								fail: function (err) {
									
								}
							});
						}
					}else{
						this.$tip(res.data.msg)
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.methodpay{
		.macon{
			>view{
				height: 112rpx;
				line-height: 112rpx;
				padding: 0 30rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				&:first-of-type{
					border-bottom: $uni-border-bottom
				}
				>image{
					width: 34rpx;
					height: 34rpx;
				}
				>view{
					display: flex;
					align-items: center;
					image{
						width: 44rpx;
						height: 44rpx;
						 margin-right: 20rpx;
					}
					text{
						font-size: 28rpx;
					}
				}
			}
		}
	}
</style>